!(() => {
    async function refresh() {
        var tokenInfo = $('#tokenInput').val();
        if(tokenInfo){
            setAuthToken(tokenInfo);
            console.log(`retry inject cookie successful...`)
        }

    }


    function setAuthToken(tokenInfo) {
        var result = JSON.parse(tokenInfo);
        var dailyToken =
            typeof result.daily_token == 'undefined' ? '' : $.trim(result.daily_token)

        chrome.runtime.sendMessage({
            action: 'setDailyToken',
            payload: dailyToken
        })

        var authJsonObj = JSON.stringify({
            username: result.username,
            created_at: result.created_at,
            email:result.username,
            nickname: result.username + ':' + result.expireTime,
            daily_token: dailyToken,
            language: result.language,
            last_checked: Date.now(),
            extension_info: result.extension_info,
            feature_access: result.feature_access
        })
        chrome.storage.local.set({ auth: authJsonObj })
        //Send message to refresh Amazon pages
        // chrome.runtime.sendMessage({
        //     action: 'refreshAmazonPages'
        // })
        //Send message to inform that the global data needs to be updated
        chrome.runtime.sendMessage({
            action: 'setGlobalData'
        })
    }

    // setInterval(refresh, 1000 * 5)

})();
